local assets =
{
    Asset("ANIM", "anim/bboy_fireworks.zip"),
    Asset("INV_IMAGE", "miniflare"),
}

local prefabs =
{
    "megaflare_minimap",
}

local function on_ignite_over(inst)
    local fx, fy, fz = inst.Transform:GetWorldPosition()

    local random_angle = math.pi * 2 * math.random()
    local random_radius = -(TUNING.MINIFLARE.OFFSHOOT_RADIUS) + (math.random() * 2 * TUNING.MINIFLARE.OFFSHOOT_RADIUS)

    fx = fx + (random_radius * math.cos(random_angle))
    fz = fz + (random_radius * math.sin(random_angle))

    -------------------------------------------------------------
    -- 创建看不见的实体,这些实体的小地图图标可以被所有玩家看到
    local minimap = SpawnPrefab("megaflare_minimap")
    minimap.Transform:SetPosition(fx + 10, fy, fz)
    minimap.color_r = 1
    minimap.color_g = 0.6
    minimap.color_b = 0.6
    minimap:DoTaskInTime(TUNING.MINIFLARE.TIME, function()
        minimap:Remove()
    end)

    local minimap = SpawnPrefab("megaflare_minimap")
    minimap.Transform:SetPosition(fx, fy, fz + 10)
    minimap:DoTaskInTime(TUNING.MINIFLARE.TIME, function()
        minimap:Remove()
    end)

    local minimap = SpawnPrefab("megaflare_minimap")
    minimap.Transform:SetPosition(fx - 5, fy, fz - 5)
    minimap:DoTaskInTime(TUNING.MINIFLARE.TIME, function()
        minimap:Remove()
    end)

    -- 检查世界状态是否允许召唤嗨猪
    local manager = TheWorld.components.bboy_piggyhimanager
    if manager ~= nil then
        if (not manager:CanSummon()) or (not TheWorld.state.isday) then
            inst:Remove()
            return
        end
    end

    -- 在实体附近生成嗨猪,延时10秒
    TheWorld:DoTaskInTime(10, function()
        -- if not TheWorld.ismastersim then
        --     return
        -- end

        -- 随机在附近 10 格内找一个合适位置
        local spawn_radius = 10
        local spawn_angle = math.random() * 2 * PI
        local spawn_offset = FindWalkableOffset(
            Vector3(fx, fy, fz),
            spawn_angle,
            math.random(5, spawn_radius), -- 5~10格之间
            8,
            true
        )

        if spawn_offset ~= nil then
            local spawn_pt = Vector3(fx + spawn_offset.x, fy, fz + spawn_offset.z)
            local pig = SpawnPrefab("bboy_piggy_hi")
            pig.Transform:SetPosition(spawn_pt:Get())

            local fx2 = SpawnPrefab("spawn_fx_medium")
            fx2.Transform:SetPosition(spawn_pt:Get())
            fx2:ListenForEvent("onremove", function()
                local beef = SpawnPrefab("bboy_carrying_beefalo")
                if beef and beef:IsValid() then
                    if beef.Physics then
                        beef.Physics:Teleport(spawn_pt:Get())
                    else
                        beef.Transform:SetPosition(spawn_pt:Get())
                    end
                end
                beef.components.follower:SetLeader(pig)
            end)

            if manager then
                manager:OnPiggySummoned()
            end

            -- 可选：添加生成特效
            local fx1 = SpawnPrefab("collapse_small")
            fx1.Transform:SetPosition(spawn_pt:Get())
        end
    end)

    inst:Remove()
end

local function on_ignite(inst)
    -- 点燃后的烟花,不再有保存机制
    inst.persists = false
    inst.entity:SetCanSleep(false)

    inst.AnimState:PlayAnimation("fire")
    inst:ListenForEvent("animover", on_ignite_over)

    inst.SoundEmitter:PlaySound("turnoftides/common/together/miniflare/launch")
end

local function on_dropped(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("idle", false)
end

local function flare_fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("bboy_fireworks")
    inst.AnimState:SetBuild("bboy_fireworks")
    inst.AnimState:PlayAnimation("idle")

    MakeInventoryFloatable(inst, "large", nil, { 0.65, 0.4, 0.65 })

    inst:AddTag("donotautopick") -- 不会被瓦器人等视作拾取目标

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("tradable")

    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")

    inst:AddComponent("burnable")
    inst.components.burnable:SetOnIgniteFn(on_ignite)

    MakeSmallPropagator(inst)
    inst.components.propagator.heatoutput = 0
    inst.components.propagator.damages = false

    MakeHauntableLaunch(inst)

    inst:ListenForEvent("ondropped", on_dropped)
    inst:ListenForEvent("floater_startfloating", function(inst) inst.AnimState:PlayAnimation("float") end)
    inst:ListenForEvent("floater_stopfloating", function(inst) inst.AnimState:PlayAnimation("idle") end)

    return inst
end

return Prefab("bboy_fireworks", flare_fn, assets, prefabs),
    MakePlacer("bboy_fireworks_placer", "bboy_fireworks", "bboy_fireworks", "idle")
